package com.lk.mall.controller.admin;

//import com.lk.common.vo.admin.UserVO;
import com.lk.mall.common.SystemConstant;
import com.lk.mall.config.annotation.TokenToAdminUser;
import com.lk.mall.controller.admin.param.AdminUserLoginParam;
import com.lk.mall.model.entity.AdminUser;
import com.lk.mall.model.entity.AdminUserToken;
import com.lk.mall.service.IAdminUserService;
import com.lk.mall.util.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author Lemonade
 * @description
 * @updateTime 2022/9/9 13:10
 */
@Api(value = "管理员相关接口", tags = "UserAPI")
@Slf4j
@RestController
@RequestMapping("/manage-api/v1")
public class UserAPI {

    @Resource
    IAdminUserService adminUserService;

    @PostMapping("/adminUser/login")
    public R<String> login(@RequestBody @Valid AdminUserLoginParam userLoginParam) {
        String loginName = userLoginParam.getUserName();
        String passwordMd5 = userLoginParam.getPasswordMd5();

        return getResult(loginName, adminUserService.login(loginName, passwordMd5), log, passwordMd5);
    }


    // -------------------------------------------------------------------------------------------------------
    public static R<String> getResult(String loginName, String loginResult, Logger log, String passwordMd5) {

        log.info("login-------------------> loginName = {}, loginResult = {}", loginName, loginResult);
        if (StringUtils.isNotBlank(loginResult) && loginResult.length() == SystemConstant.TOKEN_LENGTH) {
            return new R<String>().ok(loginResult);
        }
        if (StringUtils.equals(loginResult, SystemConstant.USER_NOT_FOUND)){
            return new R<String>().fail(SystemConstant.USER_NOT_FOUND);
        }
        if (StringUtils.equals(loginResult, SystemConstant.LOGIN_USER_LOCKED_ERROR)){
            return new R<String>().fail(SystemConstant.LOGIN_USER_LOCKED_ERROR);
        }
        return new R<String>().fail(SystemConstant.LOGIN_ERROR);
    }

    @DeleteMapping("/logout")
    public R logout(@TokenToAdminUser AdminUserToken adminUser) {
        log.info("退出登录中..............................................");
        log.info("adminUser:{}", adminUser.toString());

        Boolean logoutResult = adminUserService.logout(adminUser.getUserId());
        return new R<>().ok();
    }

    @GetMapping("/adminUser/profile")
    public R<AdminUser> profile(@TokenToAdminUser AdminUserToken adminUser) {
        log.info("退出登录中..............................................");
        log.info("adminUser:{}", adminUser.toString());

        AdminUser adminUserEntity = adminUserService.getById(adminUser.getUserId());
        if (adminUserEntity != null) {
            adminUserEntity.setLoginPassword("******");
            return new R<AdminUser>().ok(adminUserEntity);
        }
        return new R<AdminUser>().fail("未查询到记录！");
    }

    //@GetMapping
    //public R<UserVO> getUserInfo() {
    //
    //}

}
